;; Password xoauth2 stuff
(use-package auth-source-xoauth2 :ensure t)
(require 'gnus-secrets "~/.gnus-secrets.el.gpg")
(setq auth-source-xoauth2-creds 'my-xoauth2-get-secrets)
(add-to-list 'smtpmail-auth-supported 'xoauth2)
(auth-source-xoauth2-enable)
(setq gnus-asynchronous t)


;;; Sending related stuff
;; Save sent file to same group where located (using GCC-field)
(setq gnus-message-archive-method '(nnimap ""))
(setq gnus-message-archive-group
      '((lambda (group)
          (cond
           ;; Store personal mail messages in the same group I started out in
           ((string-match "nnimap.*" group) group)
           ;; Store everything else in misc until I can sort it out
           (t "mail.misc")))))



;;; Gnus agent, for speeeeeeeeed
;; go to server mode ('^' in Group buffer, or M-x
;; gnus-group-enter-server-mode) do 'J a' on any server than should be
;; handled by the agent (for speedup!!!) ----- don't do it on any
;; machines where emails shouldn't be saved as clear text on the hard
;; drive -----
;;
;; See
;; https://lists.gnu.org/archive/html/help-gnu-emacs/2020-09/msg00117.html
;; for using agent for headers but not storing whole messages (could
;; be useful for work email servers on non-work computers)
(setq gnus-agent t)
(setq gnus-agent-synchronize-flags 'ask)




;;; Level stuff (for speed etc.)
;; Read
;; https://www.gnu.org/software/emacs/manual/html_node/gnus/Group-Levels.html
;; and
;; https://emacs.stackexchange.com/questions/34537/how-to-prevent-gnus-from-querying-the-server-during-startup
(setq gnus-activate-level 5) ; Default 6, groups with level above this
                             ; won't be activated. Use 'S l' on a
                             ; group to give it a new level (if you
                             ; want to make it activated or not). Use
                             ; e.g. '6 g' to refresh all groups level
                             ; 6 or below.
(setq gnus-check-new-newsgroups nil) ; don't check server for new
                                     ; stuff. Use 'U' in group buffer
                                     ; to add new groups.

;;; Highlighting: smallest set of conditions first
(with-eval-after-load 'gnus-group
  (add-to-list 'gnus-group-highlight
               '((and mailp (>= level 4) (<= level 6))
                 . gnus-group-mail-3))
  (add-to-list 'gnus-group-highlight
               '((and mailp (= unread 0) (>= level 4) (<= level 6))
                 . gnus-group-mail-3-empty)))

;;; Interface stuff
(setq gnus-summary-line-format "%U%R %&user-date; %B%-31,31s %4k %uB\n"
      ;; Default with %f ('Name, To header or Newsgroups header')
      ;; replaced by %uB (BBDB Name + Known poster indicator '+')
      gnus-user-date-format-alist '((t . "%Y-%m-%d %H:%M"))
      gnus-summary-thread-gathering-function 'gnus-gather-threads-by-references
      gnus-summary-gather-subject-limit 'fuzzy
      gnus-fetch-old-headers t
      gnus-article-sort-functions '(gnus-article-sort-by-date)
      gnus-thread-sort-functions '((not gnus-thread-sort-by-most-recent-date))
      gnus-subthread-sort-functions '((not gnus-thread-sort-by-date))
      gnus-sum-thread-tree-false-root ""
      gnus-sum-thread-tree-indent " "
      gnus-sum-thread-tree-leaf-with-other "├► "
      gnus-sum-thread-tree-root ""
      gnus-sum-thread-tree-single-leaf "╰► "
      gnus-sum-thread-tree-vertical "│")

;; Make it so a read article marks duplicated read when seen them. So
;; articles in the backup server are marked as read (and total-expire
;; moves them to Trash) when reading them elsewhere. WARNING: reading
;; in the duplicate server marks them read elsewhere too.
(setq gnus-suppress-duplicates t)
;; Also make it so that when on auto-expiry, only unread messages that
;; are read will be automatically marked as expired. I.e. something
;; explicitly marked as read 'd' will not be marked as expired under
;; auto-expire.
(remove-hook 'gnus-mark-article-hook
             'gnus-summary-mark-read-and-unread-as-read)
;; (add-hook 'gnus-mark-article-hook
;;           'gnus-summary-mark-unread-as-read)
(add-hook 'gnus-summary-mode-hook 'my-alter-summary-map)
(defun my-alter-summary-map ()
  ;;  (keymap-local-set "E" 'gnus-summary-put-mark-as-expirable)
  ;; ^ it's better to move one down after pressing E.
;;  (keymap-local-set "d" 'gnus-summary-mark-as-read-forward)
  (keymap-local-set "?" 'gnus-summary-put-mark-as-dormant)
  (keymap-local-set "!" 'gnus-summary-put-mark-as-ticked)
  (keymap-local-set "M-u" 'gnus-summary-put-mark-as-unread))
(setq gnus-summary-goto-unread 'never) ;; go to next article (using
                                       ;; 'n' even it it's read)




;;; Security stuff
(setq mm-verify-option "always"
      gnus-buttonized-mime-types '("multipart/signed") ; show verified key etc
;;      mm-sign-option 'guided ; for always selecting signing key from a menu
      mml-secure-openpgp-encrypt-to-self t
      mml-secure-smime-encrypt-to-self t
      mml-secure-openpgp-sign-with-sender t
      mml-secure-smime-sign-with-sender t
      gnus-message-replysignencrypted t)
(add-hook 'gnus-message-setup-hook 'mml-secure-message-sign-pgpmime) ; Always sign
